//
//  YSGNurseAppointmentController.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/3/3.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit
import CoreMedia


class YSGNurseAppointmentHeadView :UIView{
    @IBOutlet weak var askButton: UIButton!
    override class func awakeFromNib() {
        super.awakeFromNib()
    }

}

class YSGNurseAppointmentController: BaseViewController {

    
    
    
    private var viewModel: YSGNurseAppointmentViewModel? {
        didSet {
               tableView.reloadData()
        }
    }
    private lazy var tableHeaderView : YSGNurseAppointmentHeadView = {
        let view : YSGNurseAppointmentHeadView  =  UINib.loadViewFrom(YSGNurseAppointmentHeadView.self)
        view.frame = CGRect(x: 0, y: 0, width: screenW, height: 226)
        view.askButton.addTarget(self, action: #selector(askButtonAction(_:)), for: .touchUpInside)
        return view
    }()

    @objc func askButtonAction(_ sender: UIButton){
        let nurseSubscribe = YSGNurseSubscribeController()
        navigationController?.pushViewController(nurseSubscribe, animated: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        tableView.tableHeaderView?.height = 201
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "护理预约"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide).priority(.low)
            make.bottom.equalToSuperview()
        }
    }
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .background
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.mj_header = refreshHeader(action: #selector(loadData))
        tableView.tableHeaderView = tableHeaderView
        tableView.mj_header?.beginRefreshing()
        tableView.register(YSGNurseAppointmentCell.self, forCellReuseIdentifier: YSGNurseAppointmentCell.reuseIdentifier())

        return tableView
    }()
    
    @objc private func loadData() {
        

        YSGNurseAppointmentViewModel.getProjectList("1"){[weak self] (models, errorMsg, code) in
            self?.tableView.mj_header?.endRefreshing()
            
            switch code {
            case .failure:
                HUDTool.showError(message: errorMsg)
            case .success:
                self?.viewModel = YSGNurseAppointmentViewModel(models: models)
            }
        }
    }

}



extension YSGNurseAppointmentController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel?.numberOfSections ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: YSGNurseAppointmentCell.reuseIdentifier(),
                                                 for: indexPath) as! YSGNurseAppointmentCell
        let model = viewModel?.models[indexPath.section]
        cell.config(model: model!)
        
        return cell
    }
}

extension YSGNurseAppointmentController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 16))
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 16
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = YSGNurseAppointmentDetailsController()
        vc.model = viewModel?.models[indexPath.section]
        navigationController?.pushViewController(vc, animated: true)
    }
}



